/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * main.c
 * Copyright (C) The Pennsylvania State University 2008 <tpk5010@psu.edu>
 * 
 * main.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/* main.c is the main application source file. Here we define our main function,
   and helper create and destroy functions for memory management. We also define
   a few locale stuffs. The main application loop is inside here, and we connect
   UI signals to their corresponding callback functions in callbacks.c/h
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <hildon-1/hildon/hildon-program.h>
#include <libintl.h>
#include <locale.h>
#include <libosso.h>
#define _(String) gettext(String)
#include <config.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include <location/location-gps-device.h>
#include <location/location-gpsd-control.h>
#include "ui/callbacks.h"
#include "appdata.h"
#include "ui/interface.h"

/*
 * Standard gettext macros.
 */
#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

/* For testing propose use the local (not installed) glade file */
/* #define GLADE_FILE PACKAGE_DATA_DIR"/nc2ifeel_nokia_client/glade/nc2ifeel_nokia_client.glade" */
#define GLADE_FILE "src/ui/nc2ifeel_nokia_client.glade"

static AppData *create_data () {

    AppData *result = g_new0 (AppData, 1);

    result->osso = osso_initialize ( "nc2ifeelnokiaclient", "0.2", TRUE, NULL );
	result->gxml = glade_xml_new( GLADE_FILE, NULL, NULL );
	/* TODO: make the gpsd control more intelligent */
	result->gpscontrol = location_gpsd_control_get_default();
	result->gpsdevice = g_object_new( LOCATION_TYPE_GPS_DEVICE, NULL );
	location_gpsd_control_start( result->gpscontrol );
	location_gps_device_start( result->gpsdevice );
	/* g_assert will cause the program to fatally exit if the statement isn't true */
	/* basically, were we able to initalize the stuffs we need? outputs to stderr */
    g_assert (result->osso);
	g_assert (result->gxml);
    return result;
}

static void destroy_data (AppData *data) {

    g_free (data);
}


int main(int argc, char *argv[]) {

 	AppData* data;
	HildonProgram* program;
	AppUIData* main_view;

/* Initalize the locale data */
#ifdef ENABLE_NLS
	bindtextdomain( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
	bind_textdomain_codeset( GETTEXT_PACKAGE, "UTF-8" );
	textdomain( GETTEXT_PACKAGE );
#endif
	
	gtk_set_locale();
	gtk_init( &argc, &argv );
	program = HILDON_PROGRAM(hildon_program_get_instance ());
	g_set_application_name( _("NC2IF Client"));

	if(!g_thread_supported()) {
		g_thread_init( NULL );
	}

	data = create_data();
	data->program = program;
	main_view = interface_main_view_new (data);

	hildon_program_add_window(data->program, data->window);

	/* This would be a good place to put signal connections. */
	glade_xml_signal_autoconnect( data->gxml );
	
	/* Handle the application close event (clicks on X in upper right */
	g_signal_connect( G_OBJECT(data->window), "delete_event", gtk_main_quit, NULL );
	
	g_signal_connect( G_OBJECT(data->gpsdevice), "changed", location_changed, main_view );
	
	/* Updates the clock */
	g_timeout_add( 500, update_clock, main_view );

	gtk_widget_show( GTK_WIDGET(data->window) );
	gtk_main ();

	interface_main_view_destroy (main_view);
	destroy_data (data);

	exit(0);
}
